<?php

/**
 * FootballFactory, Copyright Robin Loop & Pascal Wirz, 2014
 * ---
 * Repraesentiert einen Spieler
 * Methoden, um Spieler aus der DB abzurufen, editieren und loeschen
 *
 * @author Lopo
 */
class Player {
    private $id = null;
    private $firstname = null;
    private $lastname = null;
    private $team = null;
    private $status = null;

    public function __construct($data = array()) {
        if (isset($data['player_id'])) $this->id = (int) $data['player_id'];
        if (isset($data['player_firstname'])) $this->firstname = $data['player_firstname'];
        if (isset($data['player_lastname'])) $this->lastname = $data['player_lastname'];
        if (isset($data['player_team'])) $this->team = (int) $data['player_team'];
        if (isset($data['player_status'])) $this->status = (bool) $data['player_status'];
    }
	
	public static function getPlayerById($id) {
        $db = new Database();

        $sql = "SELECT * FROM ff_players WHERE player_id = :id";

        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $id, PDO::PARAM_INT);
        $stmt->execute();
        $row = $stmt->fetch();
        
        $db = null;

        if ($row) return new Player($row);
    }
	
	public static function getSeasonsByPlayerId($player_id) {
		$db = new Database();
		
		$sql = "SELECT * FROM ff_seasons_players WHERE player_id = :player_id";
		
		$stmt = $db->prepare($sql);
		$stmt->bindValue(":player_id", $player_id, PDO::PARAM_INT);
		$stmt->execute();
		
		while ($row = $stmt->fetch()) {
			$list[] = $row["season_id"];
		}
		
		$db = null;
		
		return ($list);
	}
	
	public function setSeasons($seasonIds) {
		$db = new Database();
		
		$sqlDelete = "DELETE FROM ff_seasons_players WHERE player_id = :player_id";
		$stmt = $db->prepare($sqlDelete);
		$stmt->bindValue(":player_id", $this->getId(), PDO::PARAM_INT);
		$stmt->execute();
		
		foreach ($seasonIds as $seasonId) {
			$sqlInsert = "INSERT INTO ff_seasons_players (season_id, player_id) VALUES (:season_id, :player_id)";
			$stmt = $db->prepare($sqlInsert);
			$stmt->bindValue(":season_id", $seasonId, PDO::PARAM_INT);
			$stmt->bindValue(":player_id", $this->id, PDO::PARAM_INT);
			$stmt->execute();
		}
		$db = null;
	}
	
	public static function getPlayersByTeam($team_id) {
        $db = new Database();

        $sql = "SELECT * FROM ff_players WHERE player_team = :team_id";

        $stmt = $db->prepare($sql);
        $stmt->bindValue(":team_id", $team_id, PDO::PARAM_INT);
        $stmt->execute();
        
        $list = array();
        while ($row = $stmt->fetch()) {
            $player = new Player($row);
            $list[] = $player;
        }

        $sql = "SELECT FOUND_ROWS() AS totalRows";
        $totalRows = $db->query($sql)->fetch();
        
        $db = null;
        
        return (array("playerList" => $list, "totalRows" => $totalRows[0]));
    }
	
	public static function getPlayersByTeamAndSeason($team_id, $season_id) {
        $db = new Database();

        $sql = "SELECT * FROM ff_players p, ff_seasons_players sp, ff_seasons s WHERE sp.player_id = p.player_id AND sp.season_id=s.season_id AND p.player_team = :team_id AND s.season_id = :season_id";
		
        $stmt = $db->prepare($sql);
        $stmt->bindValue(":team_id", $team_id, PDO::PARAM_INT);
        $stmt->bindValue(":season_id", $season_id, PDO::PARAM_INT);
        $stmt->execute();
        
        $list = array();
        while ($row = $stmt->fetch()) {
            $player = new Player($row);
            $list[] = $player;
        }

        $sql = "SELECT FOUND_ROWS() AS totalRows";
        $totalRows = $db->query($sql)->fetch();
        
        $db = null;
        
        return (array("playerList" => $list, "totalRows" => $totalRows[0]));
    }
	
	public static function getPlayers() {
        $db = new Database();

        $sql = "SELECT * FROM ff_players";

        $stmt = $db->prepare($sql);
        $stmt->execute();
        
        $list = array();
        while ($row = $stmt->fetch()) {
            $player = new Player($row);
            $list[] = $player;
        }

        $sql = "SELECT FOUND_ROWS() AS totalRows";
        $totalRows = $db->query($sql)->fetch();
        
        $db = null;
        
        return (array("playerList" => $list, "totalRows" => $totalRows[0]));
    }
	
	public static function getPlayersBySeason($season_id) {
        $db = new Database();

        $sql = "SELECT * FROM ff_players p, ff_seasons_players sp, ff_seasons s WHERE sp.player_id = p.player_id AND sp.season_id=s.season_id AND s.season_id = :season_id";
		
        $stmt = $db->prepare($sql);
        $stmt->bindValue(":season_id", $season_id, PDO::PARAM_INT);
        $stmt->execute();
        
        $list = array();
        while ($row = $stmt->fetch()) {
            $player = new Player($row);
            $list[] = $player;
        }

        $sql = "SELECT FOUND_ROWS() AS totalRows";
        $totalRows = $db->query($sql)->fetch();
        
        $db = null;
        
        return (array("playerList" => $list, "totalRows" => $totalRows[0]));
    }
	
	public function insert() {
        if ($this->id != 0) {
            trigger_error("Player::insert(): Es wird versucht ein neuer Spieler anzulegen, obwohl bereits die ID gesetzt wurde (ID: $this->id).", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "INSERT INTO ff_players (player_firstname, player_lastname, player_team, player_status) VALUES (:firstname, :lastname, :team, :status)";
		
		$stmt = $db->prepare($sql);
        $stmt->bindValue(":firstname", $this->firstname, PDO::PARAM_STR);
        $stmt->bindValue(":lastname", $this->lastname, PDO::PARAM_STR);
        $stmt->bindValue(":team", $this->team, PDO::PARAM_INT);
        $stmt->bindValue(":status", $this->status, PDO::PARAM_INT);
        $stmt->execute();
        
        $this->id = $db->lastInsertId();
        
        $db = null;
    }
	
	public function update() {
        if (is_null($this->id)) {
            trigger_error("Player::update(): Es wird versucht ein Spieler zu speichern, der keine ID hat - was nicht geht beim Update.", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "UPDATE ff_players SET player_firstname=:firstname, player_lastname=:lastname, player_team=:team, player_status=:status WHERE player_id=:id";
	
        $stmt = $db->prepare($sql);
        $stmt->bindValue(":firstname", $this->firstname, PDO::PARAM_STR);
        $stmt->bindValue(":lastname", $this->lastname, PDO::PARAM_STR);
        $stmt->bindValue(":team", $this->team, PDO::PARAM_INT);
        $stmt->bindValue(":status", $this->status, PDO::PARAM_INT);
        $stmt->bindValue(":id", $this->id, PDO::PARAM_INT);
        $stmt->execute();
        
        $db = null;
    }
	
	public function delete() {
        if (is_null($this->id)) {
            trigger_error("Player::delete(): Es wird versucht einen Spieler zu loeschen, der keine ID hat - was nicht geht beim Loeschen.", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "DELETE FROM ff_players WHERE spieler_id = :id LIMIT 1";
        
        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $this->id, PDO::PARAM_INT);
        $stmt->execute();
        
        $db = null;
    }
    public function getId() {
        return $this->id;
    }

    public function getFirstname() {
        return $this->firstname;
    }

    public function getLastname() {
        return $this->lastname;
    }

    public function getTeam() {
        return $this->team;
    }

    public function getStatus() {
        return $this->status;
    }


    
    
}
?>
